import dayjs from 'dayjs'

const pointList = [
    {
        "changePoint": 5,
        "changeType": 1,
        "pointType": 18,
        "pointTypeName": "访问雪王魔法铺",
        "createdTime": "2025-04-18 22:36:30"
    },
    {
        "changePoint": 5,
        "changeType": 1,
        "pointType": 3,
        "pointTypeName": "签到奖励",
        "createdTime": "2025-03-18 22:03:21"
    },
    {
        "changePoint": 5,
        "changeType": 1,
        "pointType": 18,
        "pointTypeName": "访问雪王魔法铺",
        "createdTime": "2025-02-17 22:56:14"
    },
    {
        "changePoint": 280,
        "changeType": 2,
        "pointType": 100,
        "pointTypeName": "积分过期",
        "createdTime": "2025-01-01 03:27:24"
    }
]

export default {
    namespaced: true,
    state: {
        pointList: []
    },
    mutations: {
        setPointList (state, pointList) {
            state.pointList = pointList
        }
    },
    actions: {
        getPointListAction ({ commit }, changeType) {
            const sourceList = pointList.map(item => {
                return {
                    ...item,
                    month: dayjs(item.createdTime).format("YYYY年MM月")
                }
            })

            const filteredList = sourceList.filter(item => {
                if (changeType) return item.changeType === changeType
                return true
            })

            const monthMap = {}
            filteredList.forEach(item => {
                if (!monthMap[item.month]) {
                    monthMap[item.month] = []
                }
                monthMap[item.month].push(item)
            })

            const groupedList = Object.keys(monthMap).map(month => ({
                month,
                list: monthMap[month]
            }))

            commit('setPointList', groupedList)
        }
    }
}